<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_ACTION_PATH."base_action_class.php");
    require_once(CUTEENGINE_BASE_STEP_PATH."base_step_class.php");
	//
    // t_action_file_get_contents
    //
	class t_action_file_get_contents extends t_base_action
	{
        // property declaration
		// protected        
		// G
		const language_position_last = 1;
		// C
		private $current_row_no = -1;
		// Q
		// S
		private $sql_values = null;
		// property declaration
		// public
		// P
		const proper_properties_position_per_source_type = 0;
		const proper_properties_position_per_source = 1;
		const proper_properties_position_per_query_parameters_names = 2;
        const proper_properties_position_last = 3;
		
        // R
        const required_fields_count = 1;
		// S
		const source_type_from_link = 1; //link from proper parameter
		const source_type_from_link_query = 2; // link from sql query
				
        // method declaration
    	// protected
        // G
		protected function get_argument_index()
		{
			
            if ((($this->is_executed_from_event()) || ($this->main_action != null)) /*&& 
				($local_class != 't_field_do_it')*/)
                $local_return = 3;
            else
                $local_return = -1;
            return $local_return;    
                
		} 
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
		
		protected function get_parameters_array($a_parameter)
		{
			global $g_ambient_namespace;
   			//print_r($a_parameter);
			$local_index = $this->get_argument_index();
			//echo "PARAMETER: ". $g_ambient_namespace->remove_hide_separator($a_parameter->get_argument_value($local_index))." - local_index:$local_index<br>";  
			if ($local_index == -1)
                $local_return = $g_ambient_namespace->remove_hide_separator(explode(t_url_parameter::separator_level_0,$a_parameter->get_argument_value($local_index)));
            else
                $local_return = $g_ambient_namespace->remove_hide_separator(explode(t_url_parameter::separator_level_1,$a_parameter->get_argument_value($local_index)));
            return $local_return;    
                
		} 	
		static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			//echo "a_index: $a_index<br>";
			/*switch ($a_index) 
            {
				case self::proper_properties_position_per_:
					$local_return = '';
				break;				
			}*/
			return $local_return;
		}		
        protected function get_proper_properties_position_last()
        {
            return self::proper_properties_position_last;
        }
		protected function get_required_fields_count()
        {
            return self::required_fields_count;
        }
        

		// I
        protected function initialize_action()
        {		

			if (! $this->initialized)
			{
                parent::initialize_action();
				
				

			}
			return $this->initialized;
		}		
        protected function initialize_step_execution()
        {		
			//echo "this->current_step_index: $this->current_step_index<br>";
			if ($this->current_step_index == t_base_action::step_start)
            {
                $local_step = $this->steps_list[$this->current_step_index];
                $local_step->initialize();
                $this->custom_execute_step($local_step); 
            }                                               
		}	
		protected function is_executed_from_event()
		{
			return true;
		}
        protected function custom_execute_step($a_step)        
        {
        }
        // method declaration
    	// public
        public function __construct($a_parent_component,$a_parent_part,$a_common_properties_str, $a_proper_properties_str) 
		{
            parent::__construct($a_parent_component,$a_parent_part,$a_common_properties_str, $a_proper_properties_str);
          //  echo "count:".count($this->fields_list);
			// steps
           
			$this->steps_list[t_base_action::step_start] = t_base_step::create_step_from_string('|t_step_ajax_put|'.$this->get_action_name().'_res|'.
              $this->fields_list[t_base_action::step_start].'|');  
		}
        // D
		public function do_action_from_ajax($a_result)
        {
			
			if (!isset($a_result))
                $a_result = new t_content_loader_result;
			//echo "this->proper_properties_array[self::proper_properties_position_per_source]: ".$this->proper_properties_array[self::proper_properties_position_per_source]."<br>";
			//echo "this->proper_properties_array[self::proper_properties_position_per_source_type]: ".$this->proper_properties_array[self::proper_properties_position_per_source_type]."<br>";
			if ($this->proper_properties_array[self::proper_properties_position_per_source_type] == self::source_type_from_link)
				$this->file_get_contents($a_result);
			else if ($this->proper_properties_array[self::proper_properties_position_per_source_type] == self::source_type_from_link_query)
				$this->execute_query($a_result,null);
			else
				throw new Exception('Error creating action class "'.get_class($this). '" due to wrong source type ' . $this->proper_properties_array[self::proper_properties_position_per_source_type]);
	
		}
		// F
		public function file_get_contents($a_result)
        {
			//echo "file: ".$this->proper_properties_array[self::proper_properties_position_per_source]."<br>";
			$local_field = $this->steps_list[t_base_action::step_start]->field;
			$local_result = file_get_contents($this->proper_properties_array[self::proper_properties_position_per_source]);
			//echo "local_result: $local_result<br>";
			
			if ($local_result === false)
			{
				$a_result->succeeded = false;
				$local_field->apply_error_result_script($a_result);
				
			}	
			else
			{	
				$a_result->content = $local_result;
				$a_result->succeeded = true;
				if (!$this->is_child_action())
					$local_field->apply_result_script($a_result);
			}	
			$a_result->get_json_string();			
		}

		public function file_get_contents_from_query($a_result)
        {
			
			$local_field = $this->steps_list[t_base_action::step_start]->field;
			if (($this->current_row_no)  >= count($this->sql_values))
			{
				$local_result = true;
			}
			else
			{	
				//echo "URL index($this->current_row_no):". $this->sql_values[$this->current_row_no]."<br>";
				//print_r($this->sql_values);
								
				/*$ctx = stream_context_create(array( 
					'http' => array( 
					'timeout' => 300
					) 
				) 
				);*/
				//echo "ctx"; print_r($ctx);echo "<BR>";		
			    //ini_set('default_socket_timeout',    300);
				$start = microtime(true);
				$ch = curl_init ($this->sql_values[$this->current_row_no]);
				curl_setopt($ch, CURLOPT_HEADER, 0);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
				curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
				$local_result=curl_exec($ch);
				$errno = curl_errno($ch);
				if ($errno > 0) {
					if ($errno === 28) {
						echo "Connection timed out.";
					}
					else {
						echo "Error #" . $errno . ": " . curl_error($ch);
					}
				}
				else {
					echo $output;
				}
				$end = microtime(true);
				echo "<br><br>" . ($end - $start);
				curl_close($ch);

		
				//$local_result = file_get_contents($this->sql_values[$this->current_row_no]/*, false, $ctx*/);
				echo "local_result: $local_result<BR>";
				//if ($local_result !== false)
					//$local_result  = true;
				
			}
			//echo "local_result: $local_result<br>";
			if ((get_class($local_field)) == 't_field_processes_engine')
			{
				if ($local_result === false)
				{
					$a_result->succeeded = false;
					$local_field->apply_error_result_script($a_result,$this->current_row_no,count($this->sql_values));
					
				}	
				else
				{
					//echo "OK";
					$a_result->content = $local_result;
					$a_result->succeeded = true;
					$local_field->apply_result_script($a_result,$this->current_row_no,count($this->sql_values));
				}	
						
			}
			else
			{
				if ($local_result === false)
				{
					$a_result->succeeded = false;
					$local_field->apply_error_result_script($this,$a_result,$this->current_row_no,count($this->sql_values));
					
				}	
				else
				{
					//echo "OK";
					$a_result->content = $local_result;
					$a_result->succeeded = true;
					$local_field->apply_result_script($this,$a_result,$this->current_row_no,count($this->sql_values));
				}	
			}
			if (!$this->is_child_action())
					$a_result->get_json_string();
		}		
		// E
        public function execute_query($a_result,$a_connection)
        {
			//echo "execute_query";
			global $g_ambient_namespace;
			if ($a_connection == null)
				$local_connection = $g_ambient_namespace->connection;
			else
				$local_connection = $a_connection;
			//
			
            $local_component = $this->parent_component;
			$local_parameter = parent::do_action_from_ajax();
			
			if (! isset($this->sql_values))
			{
				$local_query_name = $this->get_action_name()."_query";
				
				$local_query_text = trim($this->proper_properties_array[self::proper_properties_position_per_source]);
				
				if ($local_query_text == '')
					return;
				$local_component->new_query(
					$local_query_name,
					$local_query_text,
					$local_connection);
				
				 
				// GET QUERY
				$local_query = $local_component->get_query(
					$local_query_name);
				//echo $local_query->sql_str."<br>";
				//echo $local_query->retrive_sql_query_with_value_parameter($local_query->sql_str)."<br>";
				$local_parameters_names = $this->proper_properties_array[self::proper_properties_position_per_query_parameters_names];
				$local_parameters_array = $this->get_parameters_array($local_parameter);
				parent::apply_query_parameter_by_name($local_query,$local_parameters_array,$local_parameters_names);
				echo $local_query->retrive_sql_query_with_value_parameter($local_query->sql_str);
				$local_query->execute_function_from_query_result($this,"fill_sql_values");
				$this->current_row_no = 0;
			}
			else
			{
				echo "NEXT<br>";
				$this->current_row_no++;
				
			}
			echo "this->current_row_no: $this->current_row_no - count(this->sql_values): ".count($this->sql_values)."<br>"; 
			$this->file_get_contents_from_query($a_result);
			
			if (($this->current_row_no + 1) == count($this->sql_values))
				$this->reset();
			
		}	
		// F
        public function fill_sql_values($a_query_parameter,$a_current_row_no,$a_rows_count)
		{
			
			$this->sql_values[$a_current_row_no] = $a_query_parameter->sql_values[0];
		}
		// R
        public function reset()
        {
			echo "RESET <br>";
			$this->current_row_no = -1;
			$this->sql_values = null;
		}
    }
?>    